"""
Case Type   : cm_ctl
Case Name   : 资源池化集群执行cm_ctl pause后,查看日志和暂停标志文件
Create At   : 2024/04/05
Owner       : @kyeleze
Description :
    1.所有节点下查看暂停标志文件;
    2.执行cm_ctl pause;
    3.再次查看暂停标志文件;
    4.查看cm_agent下的cm_agent-xxx-current.log日志;
    5.执行cm_ctl resume;
    6.再次查看暂停标志文件;
Expect      :
    1.无暂停标志文件
    2.执行成功
    3.生成暂停标志文件
    4.agent日志中提示The cluster has been paused
    5.执行成功
    6.暂停标志文件被删除
History     :
    Modified by kyleze at 2025/3/10:增加多次判断，保证查询集群状态正常
"""

import os
import unittest

from yat.test import Node
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class CmPauseResumeCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.constant = Constant()
        self.pri_user = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pause_file = os.path.join('$GAUSSHOME', 'bin/cluster_manual_pause')
        self.grep_cmd = f"cd {macro.GAUSSDB_LOG_PATH}/cm/cm_agent;" \
            f"grep -c 'The cluster has been paused' cm_agent-*-current.log"
        self.log_count = self.common.get_sh_result(self.pri_user, self.grep_cmd)
        self.log.info(self.log_count)

    def test_cm(self):
        step = '--step1:所有节点下查看暂停标志文件 expect:无暂停标志文件--'
        self.log.info(step)
        result = self.primary_sh.exec_gs_ssh(f"ls {self.pause_file}")
        self.log.info(result)
        self.assertEqual(3, result.count('No such file or directory'),
                         "执行失败:" + step)
        new_log_count = self.common.get_sh_result(self.pri_user, self.grep_cmd)
        self.log.info(new_log_count)
        self.assertTrue(int(new_log_count) - int(self.log_count) == 0,
                        "执行失败:" + step)

        step = '--step2:执行cm_ctl pause expect:执行成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('pause')
        self.log.info(result)
        self.assertIn("The cluster has been paused.", result,
                      "执行失败：" + step)

        step = '--step3:再次查看暂停标志文件 expect:生成暂停标志文件--'
        self.log.info(step)
        result = self.primary_sh.exec_gs_ssh(f"ls {self.pause_file}")
        self.log.info(result)
        self.assertEqual(3, result.count('cluster_manual_pause'),
                         "执行失败:" + step)

        step = ('--step4:查看cm_agent下的cm_agent-xxx-current.log日志 '
                 'expect:agent日志中提示The cluster has been paused--')
        self.log.info(step)
        new_log_count = self.common.get_sh_result(self.pri_user, self.grep_cmd)
        self.assertTrue(int(new_log_count) - int(self.log_count) > 1,
                        "执行失败:" + step)

        step = '--step5:执行cm_ctl resume expect:执行成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl("resume")
        self.log.info(result)
        self.assertIn("The cluster has been resumed.", result,
                      "执行失败:" + step)

        step = '--step6:再次查看暂停标志文件 expect:暂停标志文件被删除--'
        self.log.info(step)
        result = self.primary_sh.exec_gs_ssh(f"ls {self.pause_file}")
        self.assertEqual(3, result.count('No such file or directory'),
                         "执行失败:" + step)

    def tearDown(self):
        step = '----恢复环境----'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl("resume")
        self.log.info(result)
        self.assertIn("The cluster has been resumed.", result,
                      "执行失败:" + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')
